<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书名" prop="bookName">
        <el-input
            v-model="queryParams.bookName"
            placeholder="请输入书名"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍作者" prop="bookAuthor">
        <el-input
            v-model="queryParams.bookAuthor"
            placeholder="请输入书籍作者"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="bookPublisher">
        <el-input
            v-model="queryParams.bookPublisher"
            placeholder="请输入出版社"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版日期" prop="publishTime">
        <el-date-picker clearable
                        v-model="queryParams.publishTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出版日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单价" prop="bookPrice">
        <el-input
            v-model="queryParams.bookPrice"
            placeholder="请输入单价"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总数" prop="bookSum">
        <el-input
            v-model="queryParams.bookSum"
            placeholder="请输入总数"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借出数量" prop="bookLend">
        <el-input
            v-model="queryParams.bookLend"
            placeholder="请输入借出数量"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍类型" prop="tag">
        <el-input
            v-model="queryParams.tag"
            placeholder="请输入书籍类型"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="isbn编号" prop="isbn">
        <el-input
            v-model="queryParams.isbn"
            placeholder="请输入isbn编号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" style="display: flex;">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            size="medium"-->
<!--            @click.native="borrowBooks"-->
<!--        >借书-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-button
          style="width: 20vw;height: 5vh;font-size: 1.7em"
          type="primary"
          plain
          size="medium"
          @click.native="borrowBooks"
      >借书
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="书籍ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="书名" align="center" prop="bookName"/>
      <el-table-column label="书籍作者" align="center" prop="bookAuthor"/>
      <el-table-column label="出版社" align="center" prop="bookPublisher"/>
      <el-table-column label="出版日期" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="bookPrice"/>
      <el-table-column label="总数" align="center" prop="bookSum"/>
      <el-table-column label="借出数量" align="center" prop="bookLend"/>
      <el-table-column label="书籍类型" align="center" prop="tag"/>
      <el-table-column label="isbn编号" align="center" prop="isbn"/>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script>
import {listBook, borrowBooks, } from "@/api/management/borrowBooks";

export default {
  name: "Book",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图书表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null,
        bookAuthor: null,
        bookPublisher: null,
        publishTime: null,
        bookPrice: null,
        bookSum: null,
        bookLend: null,
        tag: null,
        isbn: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图书列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bookName: null,
        bookAuthor: null,
        bookPublisher: null,
        publishTime: null,
        bookPrice: null,
        bookSum: null,
        bookLend: null,
        tag: null,
        isbn: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 借书*/
    borrowBooks(){
      console.log(this.ids)
      const ids = this.ids
      borrowBooks(ids).then(() => {
        this.getList();
        this.$modal.msgSuccess("借书成功");
      }).catch(() => {});
    },
  }
};
</script>
<style scoped>

.app-container {
  width: 100vw;
  height: 100vh;
  padding-top: 4%;
  /*background-image: url("@/assets/images/login-background.jpg");*/
  /*background-repeat: no-repeat;*/
  /*background-size: 100% 100%;*/
  /*!* 不设置的话页面滑动时，背景会不铺满*!*/
  /*background-attachment: fixed;*/
}
</style>
